﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class admin_updatenew : System.Web.UI.Page
{
    News n = new News();
    public string list;
    public static int currenttype;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
        {
            Function f = new Function();
            Function.CheckSession();
            DataTable dt = n.GetNewsById(Convert.ToInt32(Request["id"].ToString()));
            list = f.UpdateClass(0, int.Parse(dt.Rows[0]["classid"].ToString()));
            currenttype = Int32.Parse(dt.Rows[0]["classid"].ToString());
            this.TextBox1.Text = dt.Rows[0]["title"].ToString();
            this.TextBox2.Text = dt.Rows[0]["keyword"].ToString();
            this.TextBox4.Text = dt.Rows[0]["addtime"].ToString();
            this.FCKeditor1.Value = dt.Rows[0]["content"].ToString();
            this.Image1.ImageUrl = dt.Rows[0]["pic"].ToString();       
        }
    }

  

    protected void Button2_Click(object sender, EventArgs e)
    {
        n.Classid=int.Parse(Request["pid"].ToString());
        n.Title = this.TextBox1.Text;
        n.Keyword = this.TextBox2.Text;
        n.Content = this.FCKeditor1.Value;
        n.Pic = this.Image1.ImageUrl;
        n.Addtime =Convert.ToDateTime(this.TextBox4.Text);
        n.Id = Convert.ToInt32(Request["id"].ToString());
        n.UpdateNews(n);
        Response.Write("<script>alert('修改成功...');location.href('managenews.aspx');</script>");
    }
    
    protected void Button1_Click(object sender, EventArgs e)
    {
        if (this.File1.PostedFile != null)
        {
            Function f = new Function();
            DataTable dt = n.GetNewsById(Convert.ToInt32(Request["id"].ToString()));
            list = f.UpdateClass(0, int.Parse(dt.Rows[0]["classid"].ToString()));
            string filename = File1.PostedFile.FileName;
            int i = filename.LastIndexOf(".");//获取文件名中最后一个.的索引位置
            string ex_name = filename.Substring(i);
            if (ex_name.ToUpper() != ".GIF" && ex_name.ToUpper() != ".JPG" && ex_name.ToUpper() != ".JPEG")
            {
                Response.Write("<scirpt>alert('文件格式不正确，请选择 jpg 或者 gif 格式的图片！');</script>");
                Response.End();
            }
            DateTime now = DateTime.Now;//获取当前时间
            string savefilename = now.Millisecond.ToString() + "_" + File1.PostedFile.ContentLength.ToString() + ex_name;
            File1.PostedFile.SaveAs(Server.MapPath("upload_pic/" + savefilename));
            this.Image1.ImageUrl = "upload_pic/" + savefilename;
            Response.Write("<script>alert('上传成功');</script>");
        }
    }
}
